<!DOCTYPE html>
<html lang="en">
<head>
    <title>中医体质辨识（结果速查）</title>
    <meta charset="UTF-8" name="viewport" content=" initial-scale=1.0,user-scalable=no" />
    <script src="/js/jquery/jquery-3.4.1.js"></script>
    <script src="/js/service/common.js"></script>
    <style>
        .class1{
            display: none;
        }
    </style>
    <link rel="stylesheet" href="/css/common.css" type="text/css">
    <script>
        let totalQuestions = 0;
        window.onload = () => {
            $("#answer").addClass("class1");
            $("#ques").removeClass("class1")
            initAge($('#age'), 0, 120);
            initAge($('#height'), 70, 200);
        };
        function init(gender) {
            $.ajax({
                url: `/question/questionList?gender=${gender}`,
                type: 'POST',
                dataType: 'json',
                success: function (json) {
                    clearHtml();
                    for(let i = 0, j = json.length; i < j; i++){
                        let question = json[i];
                        if(question.type === 'radio'){
                            addItem($('#radioDiv'), question, 1);
                        }else{
                            addItem($('#chooseDiv'), question, 2);
                        }
                    }
                }
            });
        }
        function clearHtml() {
            $('#radioDiv').html('');
            $('#chooseDiv').html('');
        }
        function addItem(obj, question, flag) {
            let html = obj.html();
            let s, qId = question.id;
            if(flag === 1){
                s = `<input name=${qId} type="radio" value="1">是
                <input name=${qId} type="radio" value="2">否`;
            }else{
                s = `<input  name=${qId} type="radio" value="0">没有
                <input name=${qId} type="radio" value="1">很少
                <input name=${qId} type="radio" value="2">有时
                <input name=${qId} type="radio" value="3">经常
                <input name=${qId} type="radio" value="4">总是`;
            }
            html += `
            <lable>
                ${question.id}，${question.question}<br>
                ${s}
            </lable><br>`;
            obj.html(html);
        }
        function initAge(obj, start, end) {
            let html = '';
            for(let i = start; i < end; i ++){
                html += `<option value="${i}">${i}</option>`;
            }
            obj.html(html);
        }
        function submitAnswers(){
            let checkInputs = $("#radioDiv input:checked");
            let len = checkInputs.length;
            let answers = [];
            for(let i = 0;i < len;i ++){
                let v = checkInputs[i].value;
                answers.push(v);
            }
            checkInputs = $("#chooseDiv input:checked");
            len = checkInputs.length;
            for(let i = 0;i < len;i ++){
                let v = checkInputs[i].value;
                answers.push(v);
            }
            let gender, age, height, weight, addr, phone, history;
            let obj = $("input[name=gender]:checked").val();
            if(obj){
                gender = obj;
            }else{
                alert("请完善 性别 信息！");
                return;
            }
            obj = $('#age').val();
            if(obj){
                age = obj;
            }else{
                alert("请完善 年龄 信息！");
                return;
            }
            obj = $('#height').val();
            if(obj){
                height = obj;
            }else{
                alert("请完善 身高 信息！");
                return;
            }
            obj = $('#weight').val();
            if(obj){
                weight = obj;
            }else{
                alert("请完善 体重 信息！");
                return;
            }
            obj = $('#addr').val();
            if(obj){
                addr = obj;
            }else{
                alert("请完善 地址 信息！");
                return;
            }
            obj = $('#phone').val();
            if(obj){
                if(obj.length !== 11){
                    alert("请填写 正确的11位电话 信息！");
                    return;
                }
                phone = obj;
            }else{
                alert("请完善 电话 信息！");
                return;
            }
            obj = $('input[name=history]:checked').val();
            if(obj){
                history = obj;
            }else{
                alert("请完善 是否有脑梗死或脑出血病史 信息！");
                return;
            }
            if(answers.length < 74){
                alert("请完善所有题！");
                return;
            }
            let data = JSON.stringify({
                    "gender": gender,
                    "age": age,
                    "height": height,
                    "weight": weight,
                    "addr": addr,
                    "phone": phone,
                    "history": history,
                    answers: answers
                });
            $.ajax({
                data: data,
                url: '/question/answer',
                dataType: 'json',
                contentType: 'application/json',
                type: "POST",
                success: function (json) {
                    $("#ques").addClass("class1");
                    let answerObj = $("#answer");
                    answerObj.removeClass("class1");
                    let innerHtml = '';
                    let a = json.history;
                    if(a === 1){
                        innerHtml += `<p>有脑梗死或脑出血病史</p><br>`;
                    }
                    a = json.diagnosed;
                    if(a === 1){
                        innerHtml += `<p>有被诊断过腔隙性脑梗死或短暂性脑缺血发作</p><br>`;
                    }
                    a = json.bmi;
                    let bmiRes = a < 18.5 ? '消瘦' : a < 22.9 ? '正常' : a < 27.4 ? '超重' : '肥胖';
                    innerHtml += `<p>BMI结果：${a} (${bmiRes})</p><br>`;
                    a = json.strokeRisk;
                    innerHtml += `<p>脑卒中风险评估结果：${a}</p><br>`;
                    a = json.constitution;
                    innerHtml += `<p>各项分数：${json.resType}</p><br>`;
                    innerHtml += `<p>体质结果：${a}</p><br>`;
                    innerHtml += `<input type="button" value="查看答题结果" onclick="changeDiv()">`;
                    answerObj.html(innerHtml);
                }
            });
        }
        function changeDiv() {
            $("#answer").addClass("class1");
            $("#ques").removeClass("class1");
        }
    </script>
</head>
<body>
    <div id="ques">
        <div style="text-align: center">
            <h3>中医体质与脑卒中危险人群辨识（结果速查）</h3>
            <h3>（国家重点研发计划子课题）</h3>
            <p style="overflow-wrap: break-word; width: 80%; font-size: 12px; color: red;margin: auto; text-align: left;">
                &nbsp;&nbsp;&nbsp;&nbsp;体质和健康的关系非常密切，如果体质偏颇了就会感觉身体明显不适，而体质是可以调整的。体质既禀成于先天，亦关系于后天。只有了解自身体质，才能和自己身体对话。可以在饮食上在、生活上、心理上及早进行预防管理，实现调质防病。
                脑卒中是我国单病种致死致残率最高的疾病，本测试的目的主要对脑卒中危险人群进行流行病学调查及风险评估</p>
        </div>
        <div style="margin-left: 5%">
            <label>
                1，性别：
                <input name='gender' type="radio" value="1" onclick="init(1)">男
                <input name='gender' type="radio" value="2" onclick="init(2)">女
            </label><br>
            <label>
                2，年龄：
                <select id="age">
                </select>岁
            </label><br>
            <label>
                3，身高：
                <input id="height">cm
            </label><br>
            <label>
                4，体重
                <input id="weight" type="text">公斤
            </label><br>
            <label>
                5，住址：
                <input id="addr" type="text">
            </label><br>
            <label>
                6，联系方式：
                <input id="phone" type="number">
            </label><br>
            <label>
                7，你是否有脑梗死或脑出血病史
                <input name='history' type="radio" value="1">是
                <input name='history' type="radio" value="2">否
            </label><br>
        </div>
        <div id="radioDiv" style="margin-left: 5%">
        </div>
        <div id="chooseDiv" style="margin-left: 5%">
        </div>
        <div style="text-align: center">
            <input type="button" style="color: red; background-color: blue" value="提交结果" onclick="submitAnswers()">
        </div>
    </div>
    <div id="answer">
        <div style="text-align: center" id="answerRes">
        </div>
    </div>
</body>
</html>